package com.power.sifa.approve.executor.impl;

import com.plf.common.domain.ResponseDTO;
import com.plf.common.domain.YesNo;
import com.power.external.window.api.IWindowFeign;
import com.power.external.window.model.pojo.vo.ApproveVo;
import com.power.external.window.model.pojo.vo.FinishVo;
import com.power.sifa.util.executor.AbstractPostExecutor;
import com.power.sifa.approve.model.*;
import com.power.sifa.approve.model.enumeration.*;
import com.power.sifa.approve.service.*;
import com.power.sifa.approve.thread.ThreadPoolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 生成案件受理审批表
 */
@Component
@Slf4j
public class AdmreAuditAccept extends AbstractPostExecutor {
    // 用户id
    private String userid;
    // 环节分类标识
    private String category;
    // 用户名
    private String username;
    // 单位id
    private String sectionid;
    // 单位名称
    private String sectionname;
    // 部门id
    private String deptid;
    // 部门名称
    private String deptname;
    // 处理意见
    private String remark;
    // 行政区划
    private String adid;
    // 环节名称
    private String taskname;

    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private Apl_admre_auditService apl_admre_auditService;
    @Resource
    private IWindowFeign iWindowFeign;

    @Override
    public void init(Map<String, Object> initdata) {
        this.userid = (String) initdata.get("userid");
        this.category = (String) initdata.get("category");
        this.username = (String) initdata.get("username");
        this.sectionid = (String) initdata.get("sectionid");
        this.sectionname = (String) initdata.get("sectionname");
        this.deptid = (String) initdata.get("deptid");
        this.deptname = (String) initdata.get("deptname");
        this.remark = (String) initdata.get("remark");
        this.adid = (String) initdata.get("adid");
        this.taskname = (String) initdata.get("taskname");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object>  execute() throws Exception {
        // 1. 查询子流程的表单数据
        Apl_main mainObj = apl_mainService.getById(super.applyid);
        Apl_admre_audit auditObj = this.apl_admre_auditService.getById(super.applyid);
        Apl_main faMainObj = this.apl_mainService.getById(auditObj.getApplyid());

        // 如果需要市领导审批, 分管领导不走受理方法
        if("分管领导批示".equals(this.taskname) && YesNo.YES == auditObj.getIscitymanager()) {
            return null;
        }
        // 4 记录受理时间
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        faMainObj.setAccepttime(now);
        this.apl_mainService.saveOrUpdate(faMainObj);

        // 一窗信息^ 如果是一窗业务, 发送通知
        if (Tp_applysource.WINDOW == faMainObj.getSource()) {
            ThreadPoolUtil.executor.execute(() -> {
                // 受理结果通知
                ApproveVo appVo = new ApproveVo();
                appVo.setOpinion("准予受理");
                appVo.setResult("1"); // 1 受理 0 不予受理
                appVo.setUserid(userid);
                appVo.setUsername(username);
                appVo.setTime(now);
                ResponseDTO conRef = this.iWindowFeign.confirmAccept(auditObj.getApplyid(), appVo);
                if (0 != conRef.getCode()) {
                    log.error("向一窗推送受理结果失败 applyid -> {}", faMainObj.getId());
                }
                // 办结结果通知
                FinishVo finVo = new FinishVo();
                finVo.setOpinion("许可通过");
                finVo.setResult("1"); // 0 不予许可, 1 许可通过, 109 审批出件, 96 退件办结
                finVo.setUserid(userid);
                finVo.setUsername(username);
                finVo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                finVo.setLicenceName("行政复议受理通知书");
                finVo.setLicenceCode(mainObj.getAcceptcode());
                ResponseDTO subRes = this.iWindowFeign.submitResult(auditObj.getApplyid(), finVo);
                if (0 != subRes.getCode()) {
                    log.error("向一窗推送业务办理结果失败 applyid -> {}", faMainObj.getId());
                }
            });
        }
        return null;
    }

  }


